package com.ah.service.Impl;

import com.ah.common.R;
import com.ah.mapper.UserMapper;
import com.ah.pojo.User;
import com.ah.service.IUserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.Resource;
import java.util.List;

@DubboService
public class UserServiceImpl implements IUserService {

    @Resource
    private UserMapper userMapper;

    @Override
    public R addUser(User user) {
        int row = userMapper.insert(user);
        if (row == 1){
            return R.success();
        }
        return R.error("新增用户失败");
    }

    @Override
    public R updataUser(User user) {
        int row = userMapper.updateById(user);
        if (row == 1){
            return R.success();
        }
        return R.error("修改用户失败");
    }

    @Override
    public R deleteById(Integer id) {
        int row = userMapper.deleteById(id);
        if (row == 1){
            return R.success();
        }
        return R.error("删除用户失败");
    }

    @Override
    public R selectById(Integer id) {
        User user = userMapper.selectById(id);
        if(user == null){
            return R.error("用户数据不存在");
        }
        return R.success(user);
    }

    @Override
    public R selectAll() {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        List<User> users = userMapper.selectList(queryWrapper);
        if(users == null){
            return R.error("查询所有用户失败");
        }
        return R.success(users);
    }
}
